package com.cyy.view

import cn.hutool.core.io.FileUtil
import com.cyy.model.Constants
import com.cyy.model.GmodelModel
import com.cyy.srv.GenSrv
import com.jfinal.kit.Kv
import com.jfinal.plugin.activerecord.Db
import javafx.geometry.Orientation
import javafx.scene.control.RadioButton
import javafx.scene.control.SelectionMode
import javafx.stage.FileChooser
import tornadofx.*
import java.io.File

class DBView(val gmodel: GmodelModel, val genSrv: GenSrv) : View() {
    val ef = arrayOf(FileChooser.ExtensionFilter("choose db file", "*.db"))
    val isDBFile = booleanProperty()

    lateinit var rmp: RadioButton    // mysql port radiobutton
    // lateinit var rpp:RadioButton    // postgresql port radiobutton
    lateinit var rsp: RadioButton    // mssqlserver port radiobutton
    lateinit var rop: RadioButton    // oracle port radiobutton

    override val root = scrollpane(true, true) {
        form {
            prefWidth = 900.0
            hbox(5) {
                fieldset("请设置数据库参数", labelPosition = Orientation.VERTICAL) {

                    hbox {
                        field("username") {
                            textfield(gmodel.user) {
                                text = "root"
                                required(message = "Enter user name for your database")
                            }
                        }
                        field("password") {
                            textfield(gmodel.pwd) {
                                text = "root"
                                required(message = "Enter user name for your database")
                            }
                        }

                    }
                    field("HOST") {
                        textfield(gmodel.host) {
                            text = "localhost"
                            required(message = "Enter host for your database")
                        }
                    }
                    separator(Orientation.HORIZONTAL)
                    field("dbname") {
                        button("choose db file") {
                            enableWhen(isDBFile)
                            action {
                                gmodel.dbs.clear()
                                gmodel.tables.clear()
                                val fn = chooseFile("choose db file", ef, FileChooserMode.Single){
                                    // p初始目录为当前项目目录
                                    initialDirectory= File(File("").canonicalPath)
                                }
                                if (fn.isNotEmpty()) {
                                    gmodel.dbname.value = "${fn.first()}"
                                    gmodel.jdbcUrl.value = "jdbc:${gmodel.dbtype.value}://${gmodel.dbname.value}"
                                }
                            }
                        }
                        textfield(gmodel.dbname) {
                            text = "/soft/pboot.db"
//                                required(message = "Enter user name for your database")
                            setOnKeyReleased {
                                gmodel.jdbcUrl.value = "jdbc:${gmodel.dbtype.value}://${gmodel.dbname.value}"
                            }
                        }
                    }
                    field("jdbcUrl") {
                        textfield(gmodel.jdbcUrl) {
                            text = "jdbc:sqlite://soft/pboot.db"
                            required(message = "Enter user name for your database")
                        }
                    }
                    field("需要移除的表名前缀，用,分隔") {
                        textfield(gmodel.removedTableNamePrefixes) {
                            text = "t_, sys_,"
                        }
                    }
                }
                separator(Orientation.VERTICAL)
                //  数据库列表
                fieldset(labelPosition = Orientation.VERTICAL) {
                    //                    label(gmodel.tableCounts)
                    field("选择数据库") {
                        listview(gmodel.dbs) {
                            id="dbList"
                            visibleWhen(gmodel.dbOK)
                            selectionModel.selectionMode = SelectionMode.SINGLE
                            bindSelected(gmodel.dbname)
                            selectionModel.selectedItemProperty().addListener { _, _, _ ->
                                (if (!selectionModel.selectedItem.isNullOrEmpty()) {
                                   try{
                                       genSrv.prapare()
                                   }catch(e:Exception){
                                       println("db change failed : ${e}")
                                   }
                                })
                            }
                        }
                    }
                }
                separator(Orientation.VERTICAL)
                // 数据库中的表
                fieldset(labelPosition = Orientation.VERTICAL) {
                    label(gmodel.tableCounts)
                    field("选择的数据库中的表有") {
                        listview(gmodel.tables) {
                            enableWhen(gmodel.dbOK)
                            selectionModel.selectionMode = SelectionMode.SINGLE
//                            bindSelected(gmodel.dbname)
                            bindSelected(gmodel.leftTable)
                            selectionModel.selectedItemProperty().addListener { _, _, _ ->
                                (if (selectionModel.selectedItem.isNullOrEmpty()) {
                                    genSrv.prapare()
//                                    println(genSrv.getAllTables())
                                })
                            }
                        }
                    }
                }
                fieldset() {
                    minWidth = 40.0
                    paddingTop = 100

                    button("->") {
                        prefHeight = 50.0
                        action {
                            //                            println(gmodel.leftTable.value)
                            if (gmodel.excludedTable.contains(gmodel.leftTable.value) || gmodel.leftTable.value.isNullOrEmpty()) {
                                return@action
                            } else {
                                gmodel.excludedTable.add(gmodel.leftTable.value)
                                gmodel.tables.remove(gmodel.leftTable.value)

                            }
                        }
                    }
                    button("<-") {
                        prefHeight = 50.0
                        action {
                            //                            println(gmodel.rightTable.value)
                            if (gmodel.tables.contains(gmodel.rightTable.value) || gmodel.rightTable.value.isNullOrEmpty()) {
                                return@action
                            } else {
                                gmodel.tables.add(gmodel.rightTable.value)
                                gmodel.excludedTable.remove(gmodel.rightTable.value)
                            }
                        }
                    }
                }
                fieldset(labelPosition = Orientation.VERTICAL) {
                    field("从左侧列表选择排除的表") {
                        listview(gmodel.excludedTable) {
                            id = "extble"
                            bindSelected(gmodel.rightTable)

                            selectionModel.selectedItemProperty().addListener { _, _, _ ->
                                (if (selectionModel.selectedItem.isNullOrEmpty()) {
//                                    println(genSrv.getAllTables())
                                })
                            }
                        }
                    }

                }
            }

        }
    }
}
